package com.mifengs.mall.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mifengs.mall.R;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.ToIntentUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lihaoqi on 2017/10/31.
 */

public class TwoThreeFloAdapter extends  DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private TwoThreeItemAdapter mTwoThreeItemAdapter;
    private FloorBean floorBean;
    public TwoThreeFloAdapter(Context mContext) {
        this.mContext = mContext;
    }
    public void setFloorData(FloorBean floorBean){
        this.floorBean = floorBean;
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return  new LinearLayoutHelper();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TwoThreeViewHolder(LayoutInflater.from(mContext).inflate(R.layout.fragment_two_three_flo,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof TwoThreeViewHolder){
            TwoThreeViewHolder viewHolder = (TwoThreeViewHolder)holder;
            final RecyclerView mRecyclerView = viewHolder.mRecyclerView;
            ImageView one_img = viewHolder.one_img;
            ImageView two_img = viewHolder.two_img;
            TextView des_text = viewHolder.des_text;
            TextView pro_price_text = viewHolder.pro_price_text;
            TextView pro_name = viewHolder.pro_name;
            TextView normal_price_text = viewHolder.normal_price_text;
            TextView original_price_text = viewHolder.original_price_text;
            RelativeLayout des_rl_layout = viewHolder.des_rl_layout;
            if(floorBean !=null){
                for(int i = 0;i<floorBean.getAdverts().size();i++ ){
                    AdvertBean adverts = floorBean.getAdverts().get(i);
                    switch (i){
                        case 0:
                            ImageLoadUtil.displayImage(mContext,adverts.getImg(),one_img);
                            des_text.setText(adverts.getText());
                            pro_price_text.setText("¥"+adverts.getPrice());
                            des_rl_layout.getBackground().setAlpha(150);
                            break;
                        case 1:
                            two_img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                            ImageLoadUtil.displayImage(mContext,adverts.getImg(),two_img);
                            pro_name.setText(adverts.getText());
                            normal_price_text.setText("¥"+adverts.getPrice());
                            original_price_text.setText("¥"+adverts.getCashwtf());
                            original_price_text.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                            original_price_text .getPaint().setAntiAlias(true);
                            break;
                    }
                }

                one_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdvertBean adverts = floorBean.getAdverts().get(0);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
                two_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AdvertBean adverts = floorBean.getAdverts().get(1);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });

                List<AdvertBean> tempAdvertBeanList = floorBean.getAdverts();
                final List<AdvertBean> advertBeanList = new ArrayList<AdvertBean>();
                for(int i = 2;i<tempAdvertBeanList.size();i++){
                    AdvertBean advertBean = tempAdvertBeanList.get(i);
                    advertBeanList.add(advertBean);
                    if(advertBeanList.size() ==3){
                        break;
                    }
                }
//            mRecyclerView.addItemDecoration(new DividerGridItemDecoration(mContext));
                mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,3));
                mRecyclerView.setNestedScrollingEnabled(false);
                mTwoThreeItemAdapter= new TwoThreeItemAdapter(R.layout.item_two_three_floor,advertBeanList,mContext);
                mRecyclerView.setAdapter(mTwoThreeItemAdapter);
                mTwoThreeItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AdvertBean adverts = advertBeanList.get(position);
                        ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }


    public class TwoThreeViewHolder extends RecyclerView.ViewHolder{

        RecyclerView mRecyclerView;
        ImageView one_img;
        ImageView two_img;
        TextView pro_price_text;
        TextView des_text;
        TextView pro_name;
        TextView normal_price_text;
        TextView original_price_text;
        RelativeLayout des_rl_layout;
        RelativeLayout two_rl_layout;
        public TwoThreeViewHolder(View itemView) {
            super(itemView);
            mRecyclerView = (RecyclerView)itemView.findViewById(R.id.discount_list);
            one_img = (ImageView)itemView.findViewById(R.id.one_img);
            two_img = (ImageView)itemView.findViewById(R.id.two_img);
            pro_price_text = (TextView)itemView.findViewById(R.id.pro_price_text);
            des_text = (TextView)itemView.findViewById(R.id.des_text);
            pro_name = (TextView)itemView.findViewById(R.id.pro_name);
            normal_price_text = (TextView)itemView.findViewById(R.id.normal_price_text);
            original_price_text = (TextView)itemView.findViewById(R.id.original_price_text);
            des_rl_layout = (RelativeLayout)itemView.findViewById(R.id.des_rl_layout);

        }
    }
}
